//
//  StatusViewModel.swift
//  WeiBo
//
//  Created by wukeng on 16/8/31.
//  Copyright © 2016年 wukeng. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {
    //MARK:- 定义属性
    var status : Status?
    //MARK:- 对数据处理的属性
    var sourceText : String?
    var createdAtText : String?
    var picURLs : [NSURL] = [NSURL]()
    
    //MARK:- 数据处理
    var verifiedImage : UIImage?
    var vipImage : UIImage?
    var profileURL : NSURL?
    

    
    init(status : Status) {
        super.init()
        dealDataWithStatus(status)
    }
}

extension StatusViewModel{
    private func dealDataWithStatus(status : Status){
        self.status = status
        if let source = status.source where source != ""{
            //对来源的字符串进行处理
            let startIndex = (source as NSString).rangeOfString(">").location + 1
            let length = (source as NSString).rangeOfString("</").location - startIndex
            sourceText = (source as NSString).substringWithRange(NSRange(location: startIndex, length: length))
        }
        
        if let createdAt = status.created_at {
            //对时间进行处理
            createdAtText = NSDate.createDateString(createdAt)
        }
        if let picURLDicts = status.pic_urls {
            for picURLdict in picURLDicts{
                guard let picURLString = picURLdict["thumbnail_pic"] else{
                    continue
                }
                picURLs.append(NSURL(string: picURLString)!)
            }
        }
        //处理user里面的信息
        if let user = status.user {
            switch user.verified_type {
            case 0:
                verifiedImage = UIImage(named: "avatar_vip")
            case 2,3,5:
                verifiedImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verifiedImage = UIImage(named: "avatar_grassroot")
            default:
                verifiedImage = nil
                
            }
            //处理会员图片
            if user.mbrank > 0 && user.mbrank <= 6 {
                vipImage = UIImage(named: "common_icon_membership_level\(user.mbrank)")
            }
            let profileURLString = status.user?.profile_image_url
            profileURL = NSURL(string: profileURLString ?? "")
        }
        
        
    }
}
